<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">点击获得数据</button>
</body>
<script>
    var btn = document.getElementById("btn");
    btn.addEventListener("click",
        function() {
            var xhr = new XMLHttpRequest()
            xhr.open("GET", "data.txt", true); //注意路径问题读取data.txt文件的内容，为TRUE时表示为异步。
            xhr.send(null);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200 || xhr.status == 0) { //请求本地txt文件时状态码是0。
                        alert(xhr.responseText);
                    } else {
                        alert(xhr.status);
                    }
                }
            }
        }, false)
</script>

</html>